const LRUCache = require("../template")

function testLRUCacheBasic() {
  console.log(
    "1. 基本 get 和 put 操作测试:",
    (() => {
      const cache = new LRUCache(2)
      cache.put(1, "a")
      cache.put(2, "b")
      return cache.get(1) === "a" &&
             cache.get(2) === "b" &&
             cache.get(3) === -1
    })()
  )

  console.log(
    "2. 更新已存在的 key 测试:",
    (() => {
      const cache = new LRUCache(2)
      cache.put(1, "a")
      cache.put(2, "b")
      cache.put(1, "aa")
      return cache.get(1) === "aa" &&
             cache.get(2) === "b"
    })()
  )

  console.log(
    "3. size 和 clear 方法测试:",
    (() => {
      const cache = new LRUCache(2)
      cache.put(1, "a")
      cache.put(2, "b")
      const sizeBefore = cache.size()
      cache.clear()
      const sizeAfter = cache.size()
      const getAfterClear = cache.get(1)
      return sizeBefore === 2 &&
             sizeAfter === 0 &&
             getAfterClear === -1
    })()
  )

  console.log(
    "4. this上下文缓存测试:",
    (() => {
      const cacheObj = {
        prefix: "cache_",
        createCache: function(capacity) {
          const cache = new LRUCache(capacity)
          cache.put(1, this.prefix + "value1")
          return cache.get(1)
        }
      }
      return cacheObj.createCache(2) === "cache_value1"
    })()
  )

  console.log(
    "5. 复杂对象this绑定测试:",
    (() => {
      const cacheConfigObj = {
        config: { defaultCapacity: 3 },
        createConfigCache: function() {
          const cache = new LRUCache(this.config.defaultCapacity)
          cache.put(1, "test")
          return cache.size() === 1
        }
      }
      return cacheConfigObj.createConfigCache()
    })()
  )

  console.log(
    "6. 缓存容量限制测试:",
    (() => {
      const cache = new LRUCache(2)
      cache.put(1, "a")
      cache.put(2, "b")
      cache.put(3, "c") // 应该移除 1
      return cache.get(1) === -1 &&
             cache.get(2) === "b" &&
             cache.get(3) === "c"
    })()
  )

  console.log(
    "7. 访问顺序影响LRU测试:",
    (() => {
      const cache = new LRUCache(2)
      cache.put(1, "a")
      cache.put(2, "b")
      cache.get(1) // 访问 1，使其成为最近使用
      cache.put(3, "c") // 应该移除 2
      return cache.get(1) === "a" &&
             cache.get(2) === -1 &&
             cache.get(3) === "c"
    })()
  )

  console.log(
    "8. 更新操作影响LRU测试:",
    (() => {
      const cache = new LRUCache(2)
      cache.put(1, "a")
      cache.put(2, "b")
      cache.put(1, "aa") // 更新 1，使其成为最近使用
      cache.put(3, "c") // 应该移除 2
      return cache.get(1) === "aa" &&
             cache.get(2) === -1 &&
             cache.get(3) === "c"
    })()
  )
}

testLRUCacheBasic()
